package com.xkcoding.task.controller;

import com.alibaba.fastjson.JSON;
import com.xkcoding.task.dto.AcsDoorDto;
import com.xkcoding.task.dto.AcsDoorUploadData;
import com.xkcoding.task.dto.BackDataResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
@RequestMapping("/event")
public class HikEventTypeController {


    @PostMapping(value = "/receiver")
    public ResponseEntity<String> local(@RequestBody Object message) {
        log.info("receiver ：{}", JSON.toJSONString(message));
        AcsDoorDto acsDoorDto = JSON.parseObject(JSON.toJSONString(message), AcsDoorDto.class);
        AcsDoorDto.ParamsDTO.EventsDTO eventsDTO = acsDoorDto.getParams().getEvents().get(0);
        Integer eventType = eventsDTO.getEventType();
        if ("197162".equals(String.valueOf(eventType))) {
            AcsDoorUploadData acsDoorUploadData = acsDoorDto.buildAcsDoorUpload();
            RestTemplate restTemplate = new RestTemplate();
            HttpEntity<AcsDoorUploadData> request = new HttpEntity<>(acsDoorUploadData);
            log.info("send post request ：{}", JSON.toJSONString(request));
            BackDataResponse backDataResponse = restTemplate.postForObject("http://26.218.149.239:9012/jczpt/v3/rysjsc", request, BackDataResponse.class);
            log.info("http response ：{}", JSON.toJSONString(backDataResponse));
        }
        return ResponseEntity.ok("Event received");
    }
}
